package com.thinkgem.jeesite.quartz;

import com.thinkgem.jeesite.modules.equipment.entity.EleEquipment;
import com.thinkgem.jeesite.modules.equipment.entity.EleEquipmentCommand;
import com.thinkgem.jeesite.modules.equipment.service.EleEquipmentCommandService;
import com.thinkgem.jeesite.modules.equipment.service.EleEquipmentService;
import com.thinkgem.jeesite.modules.publish.entity.EleSourceGroupPublish;
import com.thinkgem.jeesite.modules.publish.entity.EleTempletPublish;
import com.thinkgem.jeesite.modules.publish.service.EleSourceGroupPublishService;
import com.thinkgem.jeesite.modules.publish.service.EleTempletPublishService;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class SourceStatusJob {
	@Autowired
	private EleEquipmentService eleEquipmentService;
	@Autowired
	private EleTempletPublishService templetPublishService;
	@Autowired
	private EleSourceGroupPublishService eleSourceGroupPublishService;
	@Autowired
	private EleEquipmentCommandService eleEquipmentCommandService;

	public void updateSourceStatus() {
		System.out.println("Source Status Update......................Start");
		int expiredTime = Integer.parseInt(DictUtils.getDictValue("expiredTime", "systemvalue", "60"));

		EleEquipment cond = new EleEquipment();
		long current = System.currentTimeMillis();
		 cond.setDelFlag(EleEquipment.DEL_FLAG_NORMAL);
		List<EleEquipment> equipmentList = eleEquipmentService.findList(cond);
		List<EleTempletPublish> templateList = templetPublishService.findListByEquipments(equipmentList,
				EleEquipment.DEL_FLAG_NORMAL);
		List<EleSourceGroupPublish> sourceList = eleSourceGroupPublishService.findListByEquipments(equipmentList,
				EleEquipment.DEL_FLAG_NORMAL);
		List<EleEquipmentCommand> commandList = eleEquipmentCommandService.findListByEquipments(equipmentList,
				EleEquipmentCommand.DEL_FLAG_NORMAL);
		
		
		EleEquipment condForMonitor = new EleEquipment();
		condForMonitor.setIsPublish("2");
		List<EleEquipment> equipmentListForMinitor = eleEquipmentService.findList(condForMonitor);
		for (EleEquipment tmp : equipmentListForMinitor) {
			long updateTime = tmp.getUpdateDate().getTime();
			if (current - updateTime > expiredTime * 60 * 1000) {
				eleEquipmentService.updatePublishStatus(tmp.getId(), "1");
			}
		}
		for (EleTempletPublish tmp : templateList) {
			long updateTime = tmp.getUpdateDate().getTime();
			if (current - updateTime > expiredTime * 60 * 1000) {
				templetPublishService.updatePublishStatus(tmp.getId(), "3");
			}
		}
		for (EleSourceGroupPublish tmp : sourceList) {
			long updateTime = tmp.getUpdateDate().getTime();
			if (current - updateTime > expiredTime * 60 * 1000) {
				eleSourceGroupPublishService.updatePublishStatus(tmp.getId(), "3");
			}
		}
		for (EleEquipmentCommand tmp : commandList) {
			long updateTime = tmp.getUpdateDate().getTime();
			if (current - updateTime > expiredTime * 60 * 1000) {
				eleEquipmentCommandService.delete(tmp);
			}
		}

		System.out.println("Source Status Update......................End");

	}
}
